package vsvm.gui.forms;

import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;

import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import vsvm.classifier.AbstractTestSet;
import vsvm.graphics.charts.NormalDistributionChart;
import vsvm.gui.VSVMInternalFrame;
import vsvm.math.statistics.AUCTest;
import vsvm.math.statistics.McNemarTest;
import vsvm.math.statistics.distributions.NormalDistribution;

import java.awt.FlowLayout;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import javax.swing.JLabel;

import org.apache.commons.math.MathException;
import java.awt.GridLayout;


public class AUCTestResultFrame  extends VSVMInternalFrame{

	private JPanel jContentPanel = null;
	private JPanel jTablesPanel = null;
	private JTable jCalcTable = null;
	private DefaultTableModel defaultCalcTableModel = null;  //  @jve:decl-index=0:visual-constraint=""	
	private JPanel jCountsTablePanel = null;
	private JTable jCountsTable = null;
	private DefaultTableModel defaultCountsTableModel = null;  //  @jve:decl-index=0:visual-constraint=""
	private JPanel jCalcTablePanel = null;
	private JPanel jStatisticsPanel = null;
	private JPanel jResultsPanel = null;
	private JLabel jSignificanceLabel = null;
	private JLabel jEstimateValueLabel = null;
	private JLabel jPValueLabel = null;
	private AUCTest auctest = null;
	
	
	/**
	 * This method initializes 
	 * 
	 */
	public AUCTestResultFrame(AUCTest auctest) {
		super("AUC test results");
		this.auctest = auctest;
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 */
	private void initialize() {
        this.setSize(new java.awt.Dimension(416,413));
        this.setResizable(true);        
        this.setMaximizable(true);
        this.setIconifiable(true);
        this.setClosable(true);
        this.setContentPane(getJContentPanel());			
	}

	/**
	 * This method initializes jContentPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJContentPanel() {
		if (jContentPanel == null) {
			jContentPanel = new JPanel();
			jContentPanel.setLayout(new BorderLayout());
			jContentPanel.add(getJTablesPanel(), java.awt.BorderLayout.NORTH);
			jContentPanel.add(getJStatisticsPanel(), java.awt.BorderLayout.CENTER);
		}
		return jContentPanel;
	}

	/**
	 * This method initializes jTablePanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJTablesPanel() {
		if (jTablesPanel == null) {
			jTablesPanel = new JPanel();
			jTablesPanel.setLayout(new BorderLayout());
			jTablesPanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(0,0,0,0));
			jTablesPanel.add(getJCalcTablePanel(), java.awt.BorderLayout.CENTER);			
		}
		return jTablesPanel;
	}

	/**
	 * This method initializes jCalcTable	
	 * 	
	 * @return javax.swing.JTable	
	 */
	private JTable getJCalcTable() {
		if (jCalcTable == null) {
			jCalcTable = new JTable();
			jCalcTable.setFont(new java.awt.Font("Courier New", java.awt.Font.PLAIN, 12));
			jCalcTable.setModel(getDefaultCalcTableModel());
			
		}
		return jCalcTable;
	}

	/**
	 * This method initializes defaultTableModel	
	 * 	
	 * @return javax.swing.table.DefaultTableModel	
	 */
	private DefaultTableModel getDefaultCalcTableModel() {
		if (defaultCalcTableModel == null) {
			defaultCalcTableModel = new DefaultTableModel();
			defaultCalcTableModel.setColumnCount(3);
			defaultCalcTableModel.setRowCount(3);
			AbstractTestSet[] sets = this.auctest.getTestSets();
			
			defaultCalcTableModel.setValueAt(sets[0].getName(),  0, 1);
			defaultCalcTableModel.setValueAt(sets[1].getName(), 0, 2);			
			defaultCalcTableModel.setValueAt("AUC",  1, 0);
			defaultCalcTableModel.setValueAt("Standard Error", 2, 0);
			
			
			
			double[] auc = this.auctest.getAUCs();
			double[] se  = this.auctest.getStandardErrors();
			DecimalFormatSymbols dfs = new DecimalFormatSymbols();
			dfs.setDecimalSeparator('.');
			DecimalFormat df = new DecimalFormat("0.00", dfs);
			
			for (int j = 0; j < 2; j++) {
			 	defaultCalcTableModel.setValueAt(df.format(auc[j]), 1, j + 1);
			}
			for (int j = 0; j < 2; j++) {
			 	defaultCalcTableModel.setValueAt(df.format(se[j]),  2, j + 1);
			}						
			
		}
		return defaultCalcTableModel;
	}
	
	
	/**
	 * This method initializes jCalcTablePanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJCalcTablePanel() {
		if (jCalcTablePanel == null) {
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
			gridBagConstraints.gridx = 1;
			gridBagConstraints.gridy = 1;
			gridBagConstraints.weightx = 1.0;
			gridBagConstraints.weighty = 1.0;
			gridBagConstraints.insets = new java.awt.Insets(5,21,5,21);
			jCalcTablePanel = new JPanel();
			jCalcTablePanel.setLayout(new GridBagLayout());
			jCalcTablePanel.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Calc Table", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null));
			jCalcTablePanel.add(getJCalcTable(), gridBagConstraints);
		}
		return jCalcTablePanel;
	}

	/**
	 * This method initializes jStatisticsPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJStatisticsPanel() {
		if (jStatisticsPanel == null) {
			jStatisticsPanel = new JPanel();
			jStatisticsPanel.setLayout(new BorderLayout());
			jStatisticsPanel.add(getJResultsPanel(), java.awt.BorderLayout.NORTH);
			NormalDistributionChart ndc = new NormalDistributionChart("",new NormalDistribution());
			ndc.setEstimateValue(this.auctest.getZValue());
			ndc.setCriticalArea(NormalDistributionChart.TWO_SIDED_CRITICALAREA);
			jStatisticsPanel.add(ndc, java.awt.BorderLayout.CENTER);
		}
		return jStatisticsPanel;
	}

	/**
	 * This method initializes jResultsPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJResultsPanel() {
		if (jResultsPanel == null) {
			FlowLayout flowLayout = new FlowLayout();
			flowLayout.setAlignment(java.awt.FlowLayout.LEFT);
			DecimalFormatSymbols dfs = new DecimalFormatSymbols();
			dfs.setDecimalSeparator('.');
			DecimalFormat df = new DecimalFormat("0.000", dfs);
			
			jPValueLabel = new JLabel();
			try {
				jPValueLabel.setText("p-value: " + df.format(this.auctest.getPValue()) + "\t");
				jPValueLabel.setFont(new java.awt.Font("Courier New", java.awt.Font.PLAIN, 12));
			} catch (MathException e){
				System.out.println(e.getMessage());
			}
			jPValueLabel.setBackground(java.awt.Color.white);
			jEstimateValueLabel = new JLabel();
			jEstimateValueLabel.setText("Estimate: " + df.format(this.auctest.getZValue()) + "\t");
			jEstimateValueLabel.setFont(new java.awt.Font("Courier New", java.awt.Font.PLAIN, 12));
			jEstimateValueLabel.setBackground(java.awt.Color.white);
			jSignificanceLabel = new JLabel();
			jSignificanceLabel.setText("Significance: " + df.format(this.auctest.getSignificanceLevel()) + "\t");
			jSignificanceLabel.setFont(new java.awt.Font("Courier New", java.awt.Font.PLAIN, 12));
			jSignificanceLabel.setBackground(java.awt.Color.white);
			jResultsPanel = new JPanel();
			jResultsPanel.setLayout(flowLayout);
			jResultsPanel.add(jSignificanceLabel, null);
			jResultsPanel.add(jEstimateValueLabel, null);
			jResultsPanel.add(jPValueLabel, null);
			jResultsPanel.setBackground(java.awt.Color.white);
		}
		return jResultsPanel;
	}
	
}  //  @jve:decl-index=0:visual-constraint="10,10"
